home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
tools
/
cedit.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
6KB
|
296 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* cedit -
* A simple color editor. Use the left mouse button to
* pick the color on the screen to edit, then move the sliders to
* change the color.
*
* Paul Haeberli - 1984
*
*/
#include <stdio.h>
#include <gl/gl.h>
#include <gl/device.h>
#include "port.h"
#define DISPSIZE 150
float or = -1.0;
float og = -1.0;
float ob = -1.0;
int cc = -1;
float cr, cg, cb;
int m1, m2, m3;
long xorg, yorg;
int x, y;
long xsize, ysize;
float wx, wy;
int menu;
int togglehex = 0;
main(argc,argv)
int argc;
char **argv;
{
int i, j;
if (argc>1)
setcolorsys(atoi(argv[1]));
keepaspect(1,1);
glcompat(GLC_SOFTATTACH, TRUE);
winopen("cedit");
menu = defpup("colorsys %t|rgb|cmy|hsv|hls|toggle numbers");
initmouse();
if(getplanes()<=9) /* greybase hardcoded to 512, so no good if less */
cc = 64;
else
cc = 576; /* what it used to be */
makeframe();
while (1) {
checkmouse();
if (m3) {
x = getvaluator(MOUSEX);
y = getvaluator(MOUSEY);
wx = 10.0*(x-xorg)/xsize;
wy = 10.0*(y-yorg)/ysize;
if (wy>0.0 && wy<10.0) {
if (wx>1.0 && wx<2.0) {
cr = (wy-2.0)/7.0;
drawsliders(cc,cr,cg,cb);
} else if (wx>3.0 && wx<4.0) {
cg = (wy-2.0)/7.0;
drawsliders(cc,cr,cg,cb);
} else if (wx>5.0 && wx<6.0) {
cb = (wy-2.0)/7.0;
drawsliders(cc,cr,cg,cb);
}
}
}
}
}
newcolor(c)
int c;
{
int savetoggle;
cc = c;
color(c);
rectf(7.0,2.0,9.0,9.0);
grey(0.0);
rect(7.0,2.0,9.0,9.0);
modgetmcolor(c,&cr,&cg,&cb);
or = og = ob = -1.0;
drawback();
drawsliders(cc,cr,cg,cb);
if(xsize>DISPSIZE) {
savetoggle = togglehex;
togglehex = 0;
centernum(cc,8.0,0.3);
togglehex = savetoggle;
}
}
drawsliders(c,r,g,b)
int c;
float r, g, b;
{
int changed;
changed = 0;
if (r<0.0) r = 0.0;
if (r>1.0) r = 1.0;
if (r != or) {
drawknob(0.0,or,r,rgb(1.0,0.0,0.0));
or = r;
if(xsize>DISPSIZE)
centernum((int)(r*255),1.5,0.3);
changed++;
}
if (g<0.0) g = 0.0;
if (g>1.0) g = 1.0;
if (g != og) {
drawknob(2.0,og,g,rgb(0.0,1.0,0.0));
og = g;
if(xsize>DISPSIZE)
centernum((int)(g*255),3.5,0.3);
changed++;
}
if (b<0.0) b = 0.0;
if (b>1.0) b = 1.0;
if (b != ob) {
drawknob(4.0,ob,b,rgb(0.0,0.0,1.0));
ob = b;
if(xsize>DISPSIZE)
centernum((int)(b*255),5.5,0.3);
changed++;
}
if (changed)
modmapcolor(c,r,g,b);
}
drawknob(x,old,new,c)
float x, old, new;
int c;
{
pushmatrix();
translate(x,2.0+(7.0*old),0.0);
grey(0.9);
rectf(1.2,-0.10,1.8,0.10);
popmatrix();
pushmatrix();
translate(x,2.0+(7.0*new),0.0);
color(c);
rectf(1.2,-0.10,1.8,0.10);
popmatrix();
}
makeback(x)
float x;
{
pushmatrix();
translate(x,0.0,0.0);
grey(0.9);
rectf(1.0,1.6,2.0,9.4);
grey(0.0);
rect(1.0,1.6,2.0,9.4);
popmatrix();
}
drawback()
{
makeback(0.0);
makeback(2.0);
makeback(4.0);
}
initmouse()
{
qdevice(MENUBUTTON);
qdevice(MIDDLEMOUSE);
qdevice(LEFTMOUSE);
}
checkmouse()
{
short dev, val;
int sel;
while (1) {
if (m1 != 0 || m2 != 0 || m3 != 0) {
if (!qtest())
return;
}
dev = qread(&val);
switch (dev) {
case MENUBUTTON:
sel = dopup(menu);
if (sel>0) {
if (sel == 5)
togglehex = (togglehex == 1) ? 0 : 1;
else
setcolorsys(sel);
newcolor(cc);
}
break;
case MIDDLEMOUSE:
m2 = val;
if (m2 == 0) {
x = getvaluator(MOUSEX);
y = getvaluator(MOUSEY);
wx = 10.0*(x-xorg)/xsize;
wy = 10.0*(y-yorg)/ysize;
if (wx<-0.5 || wx>10.5 || wy<-0.5 || wy>10.5)
modmapcolor(getapixel(x,y),cr,cg,cb);
}
break;
case LEFTMOUSE:
m3 = val;
if (m3 == 0) {
x = getvaluator(MOUSEX);
y = getvaluator(MOUSEY);
wx = 10.0*(x-xorg)/xsize;
wy = 10.0*(y-yorg)/ysize;
if (wx<-0.5 || wx>10.5 || wy<-0.5 || wy>10.5)
newcolor(getapixel(x,y));
}
break;
case REDRAW:
makeframe();
break;
}
}
}
makeframe()
{
reshapeviewport();
getorigin(&xorg,&yorg);
getsize(&xsize,&ysize);
grey(0.8);
clear();
ortho2(0.0,10.0,0.0,10.0);
grey(0.0);
newcolor(cc);
}
modmapcolor(c,r,g,b)
int c;
float r, g, b;
{
float fr, fg, fb;
int ir, ig, ib;
torgb(r,g,b,&fr,&fg,&fb);
rgb_to_irgb(fr,fg,fb,&ir,&ig,&ib);
gammapcolor(c,ir,ig,ib);
}
modgetmcolor(c,r,g,b)
int c;
float *r, *g, *b;
{
unsigned short cr, cg, cb;
float fr, fg, fb;
gamgetmcolor(c,&cr,&cg,&cb);
irgb_to_rgb(cr,cg,cb,&fr,&fg,&fb);
fromrgb(fr,fg,fb,r,g,b);
}
centernum(val,x,y)
int val;
float x, y;
{
char buf[128];
int pixlen;
float flen;
grey(0.8);
rectf(x-1.0,y-0.3,x+1.0,y+1.2);
grey(0.0);
if (togglehex)
sprintf(buf,"0x%02x",val);
else
sprintf(buf,"%d",val);
pixlen = strwidth(buf);
flen = (pixlen*10.0)/xsize;
cmov2(x-flen/2.0,y);
charstr(buf);
}